package com.uoko.plugins.umeng.common;

import com.uoko.plugins.umeng.config.AndroidNotification;
import com.uoko.plugins.umeng.config.PushClient;
import com.uoko.plugins.umeng.config.android.*;
import com.uoko.plugins.umeng.config.base.UmengProperties;
import com.uoko.plugins.umeng.config.ios.*;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author shaohua
 * Email shaohua@uoko.com
 * created by 2018/5/30
 */
@Component
public class UmengPushSenderServer implements UmengPushSender {

    @Autowired
    private UmengProperties umengProperties;
    private PushClient client = new PushClient();

    /**
     * 广播(broadcast): 向安装该App的所有设备发送消息。(android & ios)
     *
     * @param ticker 通知栏提示文字
     * @param title  通知标题
     * @param text   通知的内容
     * @throws Exception
     */
    @Override
    public boolean sendAllBroadcast(String ticker, String title, String text) throws Exception {
        if (sendAndroidBroadcast(ticker, title, text) && sendIOSBroadcast(ticker, text)) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 广播(broadcast): 向安装该App的所有设备发送消息。
     *
     * @param ticker 通知栏提示文字
     * @param title  通知标题
     * @param text   通知的内容
     * @throws Exception
     */
    @Override
    public boolean sendAndroidBroadcast(String ticker, String title, String text) throws Exception {
        AndroidBroadcast broadcast = new AndroidBroadcast(umengProperties.getAndroidAppKey(), umengProperties.getAndroidAppMasterSecret());
        broadcast.setTicker(ticker);
        broadcast.setTitle(title);
        broadcast.setText(text);
        broadcast.goAppAfterOpen();
        broadcast.setDisplayType(AndroidNotification.DisplayType.NOTIFICATION);
        if (umengProperties.isProductionMode()) {
            broadcast.setProductionMode();
        } else {
            broadcast.setTestMode();
        }
        //设置自定义键值对
        //broadcast.setExtraField("message", message);
        return client.send(broadcast);
    }

    /**
     * Unicast<单播> </>向指定的设备发送消息，包括向单个device_token或者单个alias发消息。
     *
     * @param device_token 单个推送，device_token
     * @param ticker       通知栏提示文字
     * @param title        通知标题
     * @param text         通知的内容*
     * @throws Exception
     */
    @Override
    public boolean sendAndroidUnicast(String device_token, String ticker,
                                      String title, String text, JSONObject message) throws Exception {
        AndroidUnicast unicast = new AndroidUnicast(umengProperties.getAndroidAppKey(), umengProperties.getAndroidAppMasterSecret());
        //  Set your device token
        unicast.setDeviceToken(device_token);
        unicast.setTicker(ticker);
        unicast.setTitle(title);
        unicast.setText(text);
        unicast.goAppAfterOpen();
        unicast.setDisplayType(AndroidNotification.DisplayType.NOTIFICATION);
        unicast.setAfterOpenAction(AndroidNotification.AfterOpenAction.go_custom);
        if (umengProperties.isProductionMode()) {
            unicast.setProductionMode();
        } else {
            unicast.setTestMode();
        }
        //设置自定义键值对
        //unicast.setCustomField(message);
        unicast.setCustomField(message.toString());
        return client.send(unicast);
    }

    /**
     * 向满足特定条件的设备集合发送消息，
     * 例如: "特定版本"、"特定地域"等。
     * 友盟消息推送所支持的维度筛选和友盟统计分析所提供的数据展示维度是一致的，后台数据也是打通的
     *
     * @throws Exception
     */
    @Override
    public boolean sendAndroidGroupcast(String ticker, String title, String text) throws Exception {
        AndroidGroupcast groupcast = new AndroidGroupcast(umengProperties.getAndroidAppKey(), umengProperties.getAndroidAppMasterSecret());
        JSONObject filterJson = new JSONObject();
        JSONObject whereJson = new JSONObject();
        JSONArray tagArray = new JSONArray();
        JSONObject testTag = new JSONObject();
        JSONObject TestTag = new JSONObject();
        testTag.put("tag", "test");
        TestTag.put("tag", "Test");
        tagArray.put(testTag);
        tagArray.put(TestTag);
        whereJson.put("and", tagArray);
        filterJson.put("where", whereJson);
        System.out.println(filterJson.toString());

        groupcast.setFilter(filterJson);
        groupcast.setTicker(ticker);
        groupcast.setTitle(title);
        groupcast.setText(text);
        groupcast.goAppAfterOpen();
        groupcast.setDisplayType(AndroidNotification.DisplayType.NOTIFICATION);
        if (umengProperties.isProductionMode()) {
            groupcast.setProductionMode();
        } else {
            groupcast.setTestMode();
        }
        return client.send(groupcast);
    }

    /**
     * 自定义播(customizedcast): 开发者通过自有的alias进行推送,
     * 可以针对单个或者一批alias进行推送，也可以将alias存放到文件进行发送。
     *
     * @param alias_type 当type=customizedcast时，必填，
     *                   alias的类型,alias_type可由开发者自定义,
     *                   开发者在SDK中,调用setAlias(alias, alias_type)时所设置的alias_type
     * @param ticker     通知栏提示文字
     * @param title      通知标题
     * @param text       通知的内容
     * @throws Exception
     */
    @Override
    public boolean sendAndroidCustomizedcast(String alias_type, String ticker, String title, String text) throws Exception {
        AndroidCustomizedcast customizedcast = new AndroidCustomizedcast(umengProperties.getAndroidAppKey(),
                umengProperties.getAndroidAppMasterSecret());
        customizedcast.setAlias("alias", alias_type);
        customizedcast.setTicker(ticker);
        customizedcast.setTitle(title);
        customizedcast.setText(text);
        customizedcast.goAppAfterOpen();
        customizedcast.setDisplayType(AndroidNotification.DisplayType.NOTIFICATION);
        if (umengProperties.isProductionMode()) {
            customizedcast.setProductionMode();
        } else {
            customizedcast.setTestMode();
        }
        return client.send(customizedcast);
    }

    /**
     * @param alias_type
     * @param ticker     通知栏提示文字
     * @param title      通知标题
     * @param text       通知的内容
     * @throws Exception
     */
    @Override
    public boolean sendAndroidCustomizedcastFile(String alias_type, String ticker, String title, String text) throws Exception {
        AndroidCustomizedcast customizedcast = new AndroidCustomizedcast(umengProperties.getAndroidAppKey(),
                umengProperties.getAndroidAppMasterSecret());
        String fileId = client.uploadContents(umengProperties.getAndroidAppKey(),
                umengProperties.getAndroidAppMasterSecret(), "aa" + "\n" + "bb" + "\n" + "alias");
        customizedcast.setFileId(fileId, alias_type);
        customizedcast.setTicker(ticker);
        customizedcast.setTitle(title);
        customizedcast.setText(text);
        customizedcast.goAppAfterOpen();
        customizedcast.setDisplayType(AndroidNotification.DisplayType.NOTIFICATION);
        if (umengProperties.isProductionMode()) {
            customizedcast.setProductionMode();
        } else {
            customizedcast.setTestMode();
        }
        return client.send(customizedcast);
    }

    /**
     * @param ticker
     * @param title
     * @param text
     * @throws Exception
     */
    @Override
    public boolean sendAndroidFilecast(String ticker, String title, String text) throws Exception {
        AndroidFilecast filecast = new AndroidFilecast(umengProperties.getAndroidAppKey(), umengProperties.getAndroidAppMasterSecret());
        String fileId = client.uploadContents(umengProperties.getAndroidAppKey(),
                umengProperties.getAndroidAppMasterSecret(), "aa" + "\n" + "bb");
        filecast.setFileId(fileId);
        filecast.setTicker(ticker);
        filecast.setTitle(title);
        filecast.setText(text);
        filecast.goAppAfterOpen();
        filecast.setDisplayType(AndroidNotification.DisplayType.NOTIFICATION);
        if (umengProperties.isProductionMode()) {
            filecast.setProductionMode();
        } else {
            filecast.setTestMode();
        }
        return client.send(filecast);
    }

    /**
     * ios 发送广播
     *
     * @param ticker
     * @param text
     * @throws Exception
     */
    @Override
    public boolean sendIOSBroadcast(String ticker, String text) throws Exception {
        IOSBroadcast broadcast = new IOSBroadcast(umengProperties.getIosAppKey(),
                umengProperties.getIosAppMasterSecret());
        broadcast.setAlert(ticker);
        broadcast.setBadge(0);
        broadcast.setSound("default");
        if (umengProperties.isProductionMode()) {
            broadcast.setProductionMode();
        } else {
            broadcast.setTestMode();
        }
        broadcast.setProductionMode();
        // Set customized fields
        broadcast.setCustomizedField("text", text);
        return client.send(broadcast);
    }

    /**
     * ios 发送广播单个
     *
     * @param device_token
     * @param ticker
     * @param text
     * @param message
     * @throws Exception
     */
    @Override
    public boolean sendIOSUnicast(String device_token, String ticker, String text, String message) throws Exception {
        IOSUnicast unicast = new IOSUnicast(umengProperties.getIosAppKey(),
                umengProperties.getIosAppMasterSecret());
        unicast.setDeviceToken(device_token);
        unicast.setAlert(ticker);
        unicast.setBadge(0);
        unicast.setSound("default");
        if (umengProperties.isProductionMode()) {
            unicast.setProductionMode();
        } else {
            unicast.setTestMode();
        }
        // Set customized fields
        unicast.setCustomizedField("text", text);
        unicast.setCustomizedField("message", message);
        return client.send(unicast);
    }

    @Override
    public boolean sendIOSGroupcast() throws Exception {
        IOSGroupcast groupcast = new IOSGroupcast(umengProperties.getIosAppKey(),
                umengProperties.getIosAppMasterSecret());
        JSONObject filterJson = new JSONObject();
        JSONObject whereJson = new JSONObject();
        JSONArray tagArray = new JSONArray();
        JSONObject testTag = new JSONObject();
        testTag.put("tag", "iostest");
        tagArray.put(testTag);
        whereJson.put("and", tagArray);
        filterJson.put("where", whereJson);
        System.out.println(filterJson.toString());

        // Set filter condition into rootJson
        groupcast.setFilter(filterJson);
        groupcast.setAlert("IOS 组播测试");
        groupcast.setBadge(0);
        groupcast.setSound("default");
        if (umengProperties.isProductionMode()) {
            groupcast.setProductionMode();
        } else {
            groupcast.setTestMode();
        }
        return client.send(groupcast);
    }

    /**
     * 自定义播(customizedcast): 开发者通过自有的alias进行推送,
     * 可以针对单个或者一批alias进行推送，也可以将alias存放到文件进行发送。
     *
     * @param alias_type 当type=customizedcast时，必填，alias的类型,alias_type可由开发者自定义,开发者在SDK中,调用setAlias(alias, alias_type)时所设置的alias_type
     * @param ticker     通知栏提示文字
     * @param text       通知的内容
     * @throws Exception
     */
    @Override
    public boolean sendIOSCustomizedcast(String alias_type, String ticker, String text) throws Exception {
        IOSCustomizedcast customizedcast = new IOSCustomizedcast(umengProperties.getIosAppKey(),
                umengProperties.getIosAppMasterSecret());
        customizedcast.setAlias("alias", alias_type);
        customizedcast.setAlert(ticker);
        customizedcast.setBadge(0);
        customizedcast.setSound("default");
        if (umengProperties.isProductionMode()) {
            customizedcast.setProductionMode();
        } else {
            customizedcast.setTestMode();
        }
        return client.send(customizedcast);
    }

    @Override
    public boolean sendIOSFilecast(String ticker) throws Exception {
        IOSFilecast filecast = new IOSFilecast(umengProperties.getIosAppKey(),
                umengProperties.getIosAppMasterSecret());
        String fileId = client.uploadContents(umengProperties.getAndroidAppKey(),
                umengProperties.getAndroidAppMasterSecret(), "aa" + "\n" + "bb");
        filecast.setFileId(fileId);
        filecast.setAlert(ticker);
        filecast.setBadge(0);
        filecast.setSound("default");
        if (umengProperties.isProductionMode()) {
            filecast.setProductionMode();
        } else {
            filecast.setTestMode();
        }
        return client.send(filecast);
    }

}
